package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.common.StatusCode;
import com.qst.crop.entity.*;
import com.qst.crop.security.entity.JwtUser;
import com.qst.crop.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Slf4j
@Api("订单相关接口")
public class ShoppingcartController {

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private SellPurchaseService sellPurchaseService;


    @PostMapping("/commitOrder/{addId}/{tmoney}")
    @Transactional
    @ApiOperation("提交订单")
    public Result commit(@PathVariable String addId, @PathVariable Double tmoney,@RequestBody List<MyCart> shoppingcarts){
        log.info("Commit {}",shoppingcarts);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtUser principal = (JwtUser) authentication.getPrincipal();
        String username = principal.getUsername();

        Purchase purchase = new Purchase();
        purchase.setOwnName(username);
        purchase.setTotalPrice(tmoney);
        purchase.setAddress(addId);
        purchase.setPurchaseType(0);
        purchase.setPurchaseStatus(1);
        purchase.setCreateTime(LocalDateTime.now());
        purchase.setUpdateTime(LocalDateTime.now());
        log.info("Commit {}",purchase);
        purchaseService.commit(purchase);
        for (MyCart shoppingcart : shoppingcarts){
            PurchaseDetail purchaseDetail = new PurchaseDetail();
            BeanUtils.copyProperties(shoppingcart,purchaseDetail);
            purchaseDetail.setPurchaseId(purchase.getPurchaseId());
            Order order = orderService.getById(shoppingcart.getOrderId());
            purchaseDetail.setUninPrice(Double.valueOf(order.getPrice()));
            purchaseDetail.setSumPrice(Double.valueOf(order.getPrice())*shoppingcart.getCount());
            log.info("Commit {}",purchaseDetail);
            purchaseDetailService.commit(purchaseDetail);
            SellPurchase sellPurchase = new SellPurchase();
            BeanUtils.copyProperties(shoppingcart,sellPurchase);
            BeanUtils.copyProperties(purchase,sellPurchase);
            BeanUtils.copyProperties(purchaseDetail,sellPurchase);
            sellPurchase.setUninPricee(purchaseDetail.getUninPrice());
            log.info("Commit {}",sellPurchase);
            sellPurchaseService.commit(sellPurchase);
        }
        return new Result<>(true, StatusCode.OK,"下单成功");
    }

}
